package com.neu.service.impl;

import java.util.ArrayList;

import com.neu.dao.IproblemDao;
import com.neu.dao.IreflectDao;
import com.neu.dao.impl.ProblemDao;
import com.neu.dao.impl.ReflectDao;
import com.neu.pojo.Model;
import com.neu.pojo.Problem;
import com.neu.service.IproblemService;

public class ProblemService implements IproblemService {
	private IproblemDao problemDao = ProblemDao.getInstance();
	private IreflectDao reflectDao = ReflectDao.getInstance();
	
	private static ProblemService problemService = new ProblemService();
	private ProblemService() {}
	public static ProblemService getInstance() {
		return problemService;
	}
	
	public boolean add(Problem p) {
		return problemDao.add(p);
	}
	
	@Override
	public ArrayList<Problem> antiRelativeList(Model m){//找出不跟m模板关联的所有问题，遍历所有问题，不关联的加进去
		ArrayList<Problem> problems = new ArrayList<>();
		
		for(Problem p: problemDao.list()) {
			if(!reflectDao.islink(m, p)) {
				problems.add(p);
			}
		}
		return problems;
	}
	
	@Override
	public boolean delete(Problem p) {
		//先删关系，再删问题
		if(reflectDao.delete(p)&&problemDao.delet(p)) {
			return true;
		}
		return false;
	}
	
	public Problem search(Problem p) {
		return problemDao.search(p);
	}
}
